﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ProjectQuaDep.Admin
{
    public partial class MediaUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ////
            //string stringMasterImageName = @"D:\MasterImage.jpg";
            //Bitmap bitmapMasterImage = new System.Drawing.Bitmap(stringMasterImageName);
            //Graphics graphicsMasterImage = Graphics.FromImage(bitmapMasterImage);

            ////Set the alignment based on the coordinates 
            //StringFormat stringformatWriteTextFormat = new StringFormat();
            //stringformatWriteTextFormat.Alignment = StringAlignment.Center;

            ////Set the font color
            //Color colorStringColor = System.Drawing.ColorTranslator.FromHtml("#000081");
            //string stringText = "IMMUZIC";
            //graphicsMasterImage.DrawString(stringText, new Font("Tahoma", 12,
            //FontStyle.Bold), new SolidBrush(colorStringColor),
            //new Point(50, 10), stringformatWriteTextFormat);
            //Response.ContentType = "image/jpeg";
            //bitmapMasterImage.Save(Response.OutputStream, ImageFormat.Jpeg);
            ////Save the new image in a physical location
            //string stringOutPutFileName = @"D:\\OutPut.jpg";
            //bitmapMasterImage.Save(stringOutPutFileName);
            //bitmapMasterImage.Dispose();
        }

        protected void cmdUpload_Click(object sender, EventArgs e)
        {
            int size = int.Parse(ddlSize.SelectedValue.ToString());
            string folder = ddlFolder.SelectedValue.ToString();
            if (fuUpload1.HasFile)
            {
                string linkserver = resizeanh(fuUpload1, "/Upload/" + folder + "/", size);
                lblImg1.Text = linkserver;
            }
        }
        private string resizeanh(FileUpload fulPhoto, string thumuc, int thumbWidth)
        {
            string linkFileRe = "";
            string pathAvatars = Server.MapPath(thumuc);
            try
            {
                int thumbHeight;
                System.Drawing.Image oImg = System.Drawing.Image.FromStream(fulPhoto.PostedFile.InputStream);
                thumbHeight = (int)(oImg.Height * ((float)thumbWidth / (float)oImg.Width));
                System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);


                Graphics oGraphic = Graphics.FromImage(oThumbNail);
                oGraphic.CompositingQuality = CompositingQuality.HighSpeed;
                oGraphic.SmoothingMode = SmoothingMode.HighSpeed;
                oGraphic.InterpolationMode = InterpolationMode.High;

                Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
                oGraphic.DrawImage(oImg, oRectangle);
                string nameAdd = DateTime.Now.ToString("yyyyMMddMMhhss");
                string filesource = pathAvatars + thumbWidth + nameAdd + fulPhoto.FileName;

                oThumbNail.Save(filesource, ImageFormat.Jpeg);
                oImg.Dispose();
                linkFileRe = thumuc + thumbWidth + nameAdd + fulPhoto.FileName;
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            return linkFileRe;
        }
    }
}